// 读取

const fs = require('fs')
const { resolve } = require('path')
const { writeTxtToXls } = require('./writeTxtToXls')
try {

    const readPath = resolve(__dirname, 'src', '../asset', 'cn.txt')
    const data = fs.readFileSync(readPath, 'UTF-8');
    // 逐行读取
    const lines = data.split(/\r?\n/);
    let done = []
    let result = [
        ['词条(中文)', '词条(英文)', '翻译问题', '词条异议']
    ]

    lines.forEach((line) => {

        // 读取行内中文
        let regexp = /[\u4e00-\u9fa5]+(\?|!)*/g;
        let matchResult = line.match(regexp);
        if (matchResult instanceof Array) {
            matchResult.forEach(v => {
                let tempt = done.find(existWord => existWord == v)
                if (!tempt) {
                    done.push(v)
                }
            })

        }
    });

    done = done.map(v => ([
        v,
        '',
        '',
        '',
    ]))

    result = [
        ...result,
        ...done
    ]

    // 将词条写入xls
    const time = new Date();
    const sheetName = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()}i18n待翻译文件`
    writeTxtToXls(sheetName, result,)
} catch (err) {
    console.error(err);
}